package org.springblade.auth.social.wechatEnterprise.utils;

import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeChatEnterpriseQrcodeRequest;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.system.entity.ConfigWechatEnterprise;
import org.springblade.system.feign.ISysClient;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;

public class WechatEnterpriseUtil {
	private static AuthStateCache authStateCache;

	public WechatEnterpriseUtil() {
	}

	public static AuthStateCache getAuthStateCache() {
		if (authStateCache == null) {
			authStateCache = SpringUtil.getBean(AuthStateCache.class);
		}

		return authStateCache;
	}

	public static AuthRequest getBindingAuthRequest(String tenantId, String userId, ISysClient sysClient) {
		R<ConfigWechatEnterprise> result = sysClient.getConfigWechatEnterprise(tenantId);
		AuthConfig authConfig = new AuthConfig();
		if (result.isSuccess()) {
			ConfigWechatEnterprise configWechatEnterprise = result.getData();
			if (configWechatEnterprise == null || configWechatEnterprise.getId() == null) {
				throw new InvalidGrantException("未获取到有效的企业微信相关配置，请联系管理员");
			}
			authConfig.setAgentId(configWechatEnterprise.getAgentId());
			authConfig.setClientId(configWechatEnterprise.getClientId());
			authConfig.setClientSecret(configWechatEnterprise.getClientSecret());
			authConfig.setRedirectUri(configWechatEnterprise.getBindingUri() + "%3fuserId%3d" + userId + "%26tenantId%3d" + tenantId);
			authConfig.setIgnoreCheckState(true);
		}
		return authRequest(authConfig);
	}


	public static AuthRequest getAuthRequest(String tenantId, ISysClient sysClient) {
		R<ConfigWechatEnterprise> result = sysClient.getConfigWechatEnterprise(tenantId);
		AuthConfig authConfig = new AuthConfig();
		if (result.isSuccess()) {
			ConfigWechatEnterprise configWechatEnterprise = result.getData();
			if (configWechatEnterprise == null || configWechatEnterprise.getId() == null) {
				throw new InvalidGrantException("未获取到有效的企业微信相关配置，请联系管理员");
			}
			authConfig.setAgentId(configWechatEnterprise.getAgentId());
			authConfig.setClientId(configWechatEnterprise.getClientId());
			authConfig.setClientSecret(configWechatEnterprise.getClientSecret());
			authConfig.setRedirectUri(configWechatEnterprise.getRedirectUri() + "%3ftenantId%3d" + tenantId);
			authConfig.setIgnoreCheckState(true);
		}

		return authRequest(authConfig);
	}

	public static AuthRequest authRequest(AuthConfig authConfig) {
//		AuthRequest authRequest = null;
//		AuthDefaultSource authSource = Objects.requireNonNull(AuthDefaultSource.valueOf(source.toUpperCase()));
//		switch (authSource) {
//			case WECHAT_ENTERPRISE:
//				authRequest = new AuthWeChatEnterpriseQrcodeRequest(authConfig, getAuthStateCache());
//				break;
//		}
		AuthRequest authRequest = new AuthWeChatEnterpriseQrcodeRequest(authConfig, getAuthStateCache());
		if (null == authRequest) {
			throw new AuthException("未获取到有效的Auth配置");
		} else {
			return authRequest;
		}
	}

}
